<?php

declare(strict_types=1);

namespace app\common\controller;

use app\common\controller\Base;
use think\Request;

class Upload extends Base
{
    protected $request;
    protected $env;
    protected $service;
    protected $validate;
    protected $directory;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new \app\common\service\Upload();
        $this->directory =  config('filesystem.file.image.directory');
        $this->env = env('APP_ENV', 'dev');
    }


    /**
     * @describe
     * @author: Bobo
     * @date: 2024-06-28 11:38:15
     * @param [type] @required
     * @return Renderable
     */
    public function upload()
    {
        if (!$this->request->post('file')) {
            return $this->returnJsonData(100041);
        }
        if (!$this->request->post('directory') || !isset($this->directory[$this->request->post('directory')])) {
            return $this->returnJsonData(100042);
        }
        $file = trim($this->request->post('file'));
        $directory = intval($this->request->post('directory'));
        $res = $this->service->uploads($file, $directory);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2024-08-26 19:12:29
     * @param [type] @required
     * @return Renderable
     */
    public function uploadFile()
    {
        if (!$this->request->file()) {
            return $this->returnJsonData(100041);
        }
        if (!$this->request->post('directory') || !isset($this->directory[$this->request->post('directory')])) {
            return $this->returnJsonData(100042);
        }

        $directory = intval($this->request->post('directory'));
        $is_mark = intval($this->request->post('is_mark', 0));
        $file = $this->request->file('file');
        $fileType =  $file->getMime();

        switch ($fileType) {
            case 'image/jpeg':
            case 'image/png':
            case 'image/gif':
            case 'image/webp':
                $isThumb = intval($this->request->post('is_thumb'));
                $res = $this->service->uploadImages($file, $directory, 0, 0, $is_mark);
                break;
            case 'application/pdf':
                $res = $this->service->uploadFile($file, $directory, $is_mark);
                break;
            case 'application/msword':
                $res = $this->service->uploadFile($file, $directory, $is_mark);
                break;
            case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
                $res = $this->service->uploadFile($file, $directory, $is_mark);
                break;
            case 'application/vnd.ms-excel':
                $res = $this->service->uploadFile($file, $directory, $is_mark);
                break;
            case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
                $res = $this->service->uploadFile($file, $directory, $is_mark);
                break;
            case 'application/vnd.ms-powerpoint':
                $res = $this->service->uploadFile($file, $directory, $is_mark);
                break;
            case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
                $res = $this->service->uploadFile($file, $directory, $is_mark);
                break;
            default:
                $res = $this->service->uploadFile($file, $directory);
                break;
        }

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * @describe 批量上传文件
     * @author: Bobo
     * @date: 2025-07-21 13:14:16 
     * @param [type] @required
     * @return Renderable
     */
    public function batchUploadFile()
    {
        if (!$this->request->file()) {
            return $this->returnJsonData(100041);
        }
        // 验证目录参数
        if ($this->request->post('directory') < 0 || !isset($this->directory[$this->request->post('directory')])) {
            return $this->returnJsonData(100042);
        }
        $files = $this->request->file('files');

        $directory = intval($this->request->post('directory'));
        $is_mark = intval($this->request->post('is_mark', 0));

        if ($this->env == 'dev') $directory = 0;

        $res = $this->service->batchUploadFile($files, $directory, $is_mark);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 获取OSS直传参数
     * @author: Bobo
     * @date: 2025-07-03 16:41:23 
     * @param [type] @required
     * @return Renderable
     */
    public function getOssDirectUploadParams()
    {
        $directory = intval($this->request->param('directory'));
        if (!$directory) {
            return $this->returnJsonData(100042);
        }


        $res = $this->service->getOssDirectUploadParams($directory);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }


    /**
     * @describe 保存OSS图片至数据库
     * @author: Bobo
     * @date: 2025-07-04 15:42:32 
     * @param [type] @required
     * @return Renderable
     */
    public function saveDirectUploadInfo()
    {
        $url = $this->request->param('url');
        $directory = intval($this->request->param('directory'));
        $originalName = $this->request->param('name');
        $res = $this->service->saveDirectUploadInfo($url, $directory, $originalName);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 批量保存OSS图片至数据库
     * @author: Bobo
     * @date: 2025-07-21 16:12:37 
     * @param [type] @required
     * @return Renderable
     */
    public function batchSaveDirectUploadInfo()
    {
        $images = $this->request->param('images');
        $res = $this->service->batchSaveDirectUploadInfo($images);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
}
